import dayjs from 'dayjs'

// 隐藏手机号
export const hidePhoneNum = (phone)=> {
	let _phone = '';
	if(phone){
		let _length = phone.length;
		_phone = phone.slice(0,3)+'****'+phone.slice(_length-4,_length);
	}
    return _phone;
}

// 隐藏卡号
export const hideCardNum = (card)=> {
	let _card = '';
	if(card){
		let _length = card.length;
		_card = card.slice(0,3)+' **** **** '+card.slice(_length-4,_length);
	}
    return _card;
}

// 时间戳计算日期
export const formatDate = (date: Date, format?: string): string => {
  // 日期不存在，则返回空
  if (!date) {
    return ''
  }
  // 日期存在，则进行格式化
  if (format === undefined) {
    format = 'YYYY-MM-DD HH:mm:ss'
  }
  return dayjs(date).format(format)
}

// 距离计算km
export const formatDistance = (distance: Number) => {
	if(distance){
		return distance<1000?distance + 'm' : (distance/1000).toFixed(2) + 'km'
	}else{
		return '未知'
	}
}

// 价格保留小数点后两位
export const formatPrice = (price: Number) => {
	if(price){
		return price.toFixed(2)
	}else{
		return '0.00'
	}
}

// oosImg压缩
export const oosImgZip = (url: String) => {
	if(url&&url.indexOf('.oos')>0){
		return `${url}@oosImage|70p`
	}else{
		return url
	}
}

// 计算时间多久前
export const formatTimeAgo = (time, num = 1) => {
	let timeArr = [365*24*60*60*1000, 30*24*60*60*1000, 7*24*60*60*1000, 24*60*60*1000, 60*60*1000,60*1000, 1000];
	let unit = ["年", "个月", "周", "天", "小时", "分钟", "秒钟"];
	let timeDiff = Date.now() - new Date(time).getTime();
	if (timeDiff <= 0) return '刚刚'
	let tip = [];
	for(let i= 0; i< timeArr.length; i++){
		if(timeDiff < timeArr[i]) continue;
 
		let temp = Math.floor(timeDiff / timeArr[i]);
		timeDiff = timeDiff % timeArr[i];
 
		if(temp > 0){
			tip.push(temp + unit[i]);
		}
 
		if(tip.length >= num) break;
	}
	return tip.join("")+"前"
}